package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Area;
import com.wm.bean.City;
import com.wm.bean.Scriptorium;
import com.wm.comm.Constant;
import com.wm.comm.PinYinUtil;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.ScriptoriumService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.ui.OptionsInt;
import com.wm.web.ui.OptionsLong;

public class AdminScriptorium extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminScriptorium.class);
	
	private CityService cityService;
	private AreaService areaService;
	private ScriptoriumService scriptoriumService;

	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}
	
	public AreaService getAreaService() {
		return areaService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}
	
	public ScriptoriumService getScriptoriumService() {
		return scriptoriumService;
	}

	public void setScriptoriumService(ScriptoriumService scriptoriumService) {
		this.scriptoriumService = scriptoriumService;
	}




	private List cityList = new ArrayList();
	private List areaList = new ArrayList();
	private List scriptoriumTypeList = new ArrayList();
	private List isShowList = new ArrayList();

	
	public List getCityList() {
		return cityList;
	}

	public void setCityList(List cityList) {
		this.cityList = cityList;
	}

	public List getAreaList() {
		return areaList;
	}

	public void setAreaList(List areaList) {
		this.areaList = areaList;
	}

	public List getScriptoriumTypeList() {
		return scriptoriumTypeList;
	}

	public void setScriptoriumTypeList(List scriptoriumTypeList) {
		this.scriptoriumTypeList = scriptoriumTypeList;
	}

	public List getIsShowList() {
		return isShowList;
	}

	public void setIsShowList(List isShowList) {
		this.isShowList = isShowList;
	}
	
	private void setCityListValues(){
		cityList.add(new OptionsLong(-1, "请选择"));
		List l = this.getCityService().findCitysByIsShow(1, "id", 0);
		for(int i=0;i<l.size();i++){
			City c = (City) l.get(i);
			cityList.add(new OptionsLong(c.getId(), c.getCityName()));
		}
	}
	private void setAreaListValues(){
		areaList.add(new OptionsLong(-1, "请选择"));
		if(this.cityId!=-1){
			List l = this.getAreaService().findAreasByCityIdIsShow(this.cityId, 1, "id", 0);
			for(int i=0;i<l.size();i++){
				Area a = (Area) l.get(i);
				areaList.add(new OptionsLong(a.getId(), a.getAreaName()));
			}
		}
	}
	
	private void setScriptoriumTypeListValues(){
		scriptoriumTypeList = Constant.SCRIPTORIUM_TYPE;
	}
	
	private void setIsShowListValues(){
		isShowList = Constant.IS_SHOW;
	}
	
	private void initValues(){
		setIsShowListValues();
		setCityListValues();
		setAreaListValues();
		this.setAreas(this.getAreaService().findAreasByIsShow(1, "id", 0));
		setScriptoriumTypeListValues();
	}
	
	private void listInitValues(){
		setIsShowListValues();
		setCityListValues();
		setAreaListValues();
		this.setAreas(this.getAreaService().findAreasByIsShow(1, "id", 0));
	}


	private long id;					//主键ID
	private String scrName = "";		//写字楼名称
	private String scrInitials;			//首字母
	//private String scrAnglic;			//写字楼的简拼
	//private String scrPhonetic;		//写字楼的全拼
	private int scrType;				//写字楼类型 1:写字间 2:住宅
	private long cityId = -1;			//城市ID
	private long areaId = -1;			//地区ID
	private int isShow = -1;			//是否显示
	private int orderNum;				//排序字段

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getScrName() {
		return scrName;
	}

	public void setScrName(String scrName) {
		this.scrName = scrName;
	}

	public String getScrInitials() {
		return scrInitials;
	}

	public void setScrInitials(String scrInitials) {
		this.scrInitials = scrInitials;
	}

	public int getScrType() {
		return scrType;
	}

	public void setScrType(int scrType) {
		this.scrType = scrType;
	}

	public long getCityId() {
		return cityId;
	}

	public void setCityId(long cityId) {
		this.cityId = cityId;
	}

	public long getAreaId() {
		return areaId;
	}

	public void setAreaId(long areaId) {
		this.areaId = areaId;
	}

	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}

	public int getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}

	private List areas;
	private PageList pageList;
	
	public List getAreas() {
		return areas;
	}

	public void setAreas(List areas) {
		this.areas = areas;
	}

	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		listInitValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_scriptorium.action?scrName="+this.scrName+"&cityId=" + this.cityId + "&areaId=" + this.areaId + "&isShow=" + this.isShow);
		this.setPageList(this.getScriptoriumService().findScriptoriumsByScrNameCityIdAreaIdIsShow(this.scrName,this.cityId,this.areaId,this.isShow,"id", 1, pages));
		return SUCCESS;
	}
	
	public String add(){
		initValues();
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//添加
		Scriptorium scriptorium = new Scriptorium();
		scriptorium.setScrName(this.scrName);
		scriptorium.setScrInitials(this.scrInitials.toUpperCase());
		scriptorium.setScrAnglic(PinYinUtil.getPinYinHeadChar(this.scrName));
		scriptorium.setScrPhonetic(PinYinUtil.getPinYin(this.scrName));
		scriptorium.setScrType(this.scrType);
		scriptorium.setCityId(this.cityId);
		scriptorium.setAreaId(this.areaId);
		scriptorium.setIsShow(this.isShow);
		scriptorium.setOrderNum(this.orderNum);
		scriptorium.setOrderTime(new Date());
		scriptorium.setAddTime(new Date());
		try {
			this.getScriptoriumService().saveScriptorium(scriptorium);
		} catch (Exception e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		initValues();
		this.setAction("doupdate");
		Scriptorium scriptorium = this.getScriptoriumService().findScriptoriumById(this.id);
		this.setId(scriptorium.getId());
		this.setScrName(scriptorium.getScrName());
		this.setScrInitials(scriptorium.getScrInitials());
		//this.setScrAnglic(scriptorium.getScrAnglic());
		//this.setScrPhonetic(scriptorium.getScrPhonetic());
		this.setScrType(scriptorium.getScrType());
		this.setCityId(scriptorium.getCityId());
		this.setAreaId(scriptorium.getAreaId());
		this.setIsShow(scriptorium.getIsShow());
		this.setOrderNum(scriptorium.getOrderNum());
		return INPUT;
	}
	
	public String doupdate(){
		//添加
		Scriptorium scriptorium = this.getScriptoriumService().findScriptoriumById(this.id);
		scriptorium.setScrName(this.scrName);
		scriptorium.setScrInitials(this.scrInitials.toUpperCase());
		scriptorium.setScrAnglic(PinYinUtil.getPinYinHeadChar(this.scrName));
		scriptorium.setScrPhonetic(PinYinUtil.getPinYin(this.scrName));
		scriptorium.setScrType(this.scrType);
		scriptorium.setCityId(this.cityId);
		scriptorium.setAreaId(this.areaId);
		scriptorium.setIsShow(this.isShow);
		scriptorium.setOrderNum(this.orderNum);
		scriptorium.setOrderTime(new Date());
		scriptorium.setAddTime(new Date());
		try {
			this.getScriptoriumService().updateScriptorium(scriptorium);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String orders(){
		Scriptorium scriptorium = this.getScriptoriumService().findScriptoriumById(this.id);
		scriptorium.setOrderTime(new Date());
		try {
			this.getScriptoriumService().updateScriptorium(scriptorium);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getScriptoriumService().removeScriptoriumById(this.id);
		return "index";
	}
	
	/**
	 * 弹出
	 */
	public String pop(){
		listInitValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_scriptorium.action?action=pop&scrName="+this.scrName+"&cityId=" + this.cityId + "&areaId=" + this.areaId + "&isShow=" + this.isShow);
		this.setPageList(this.getScriptoriumService().findScriptoriumsByScrNameCityIdAreaIdIsShow(this.scrName,this.cityId,this.areaId,this.isShow,"id", 1, pages));
		return "pop";
	}
}
